package org.etom.impl.ui.wizards;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The abstract class initializes the GUI for the given wizard page.
 */
public abstract class AbstractDownloadAsWizardPage extends WizardPage {

  private static final String PAGE_NAME = "DownloadAsWizardPage"; //$NON-NLS-1$
  
  private Composite composite;
  protected Button newProjectWizardButton;
  protected Button newProjectButton;
  protected Button inExistingProjectButton;
  protected Text newProjectNameText;
  
  protected AbstractDownloadAsWizardPage() {
    super(PAGE_NAME);
  }

  /* ===[Wizard Page methods][Start]=== */
  @Override
  public void createControl(Composite parent) {
    initComposite(parent);
    addLabels();
    this.newProjectWizardButton = createRadioButton(
        WizardsMessages.DownloadAs_Wizard_NewProjectWizard);
    this.newProjectButton = createRadioButton(
        WizardsMessages.DownloadAs_Wizard_NewProject);
    addNewProjectControls();
    //TODO: will be done later
//    this.inExistingProjectButton = createRadioButton(
//        WizardsMessages.DownloadAs_Wizard_InExistingProject);
    initPage();
  }
  /* ===[Wizard Page methods][End]=== */
  
  private void initComposite(Composite parent) {
    this.composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    this.composite.setLayout(layout);
  }
  
  private void addLabels() {
    Label moduleLabel = new Label(composite, SWT.NONE);
    moduleLabel.setText(WizardsMessages.DownloadAs_Wizard_Module_Label); //TODO: add module name here
    addMergeLayoutData(moduleLabel);
  }
  
  private Button createRadioButton(String message) {
    Button button = new Button(composite, SWT.RADIO);
    button.setText(message);
    addMergeLayoutData(button);
    return button;
  }
  
  private void addNewProjectControls() {
    Label projectNameLabel = new Label(composite, SWT.NONE);
    projectNameLabel.setText(WizardsMessages.DownloadAs_Wizard_NewProjectName);
    GridData projectNameLabelGridData = new GridData();
    projectNameLabelGridData.horizontalIndent = 5;
    projectNameLabel.setLayoutData(projectNameLabelGridData);

    this.newProjectNameText = new Text(composite, SWT.BORDER);
    GridData newProjectNameTextGridData = new GridData();
    newProjectNameTextGridData.grabExcessHorizontalSpace = true;
    newProjectNameTextGridData.horizontalAlignment = SWT.FILL;
    this.newProjectNameText.setLayoutData(newProjectNameTextGridData);
  }
  
  private void initPage() {
    setControl(this.composite);
    setTitle(WizardsMessages.DownloadAs_Wizard_Title);
    setDescription(WizardsMessages.DownloadAs_Wizard_Description);
  }
  
  private static void addMergeLayoutData(Control control) {
    GridData gridData = new GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalSpan = 2;
    control.setLayoutData(gridData);
  }
  
}
